<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@include file="/context/mytags.jsp" %>
<t:base type="jquery,easyui,tools,DatePicker"></t:base>
<script type="text/javascript">
    $(function () {
        mallOilcardRecoveryModule.init();
    });
    var mallOilcardRecoveryModule = (function () {

        /*初始化时间查询*/
        var initDate = function () {
            $("input[name='createTime_begin']").attr("class", "Wdate").click(function () {
                WdatePicker({dateFmt: 'yyyy-MM-dd HH:mm:ss'});
            });
            $("input[name='createTime_end']").attr("class", "Wdate").click(function () {
                WdatePicker({dateFmt: 'yyyy-MM-dd HH:mm:ss'});
            });

        }

        var setStatisticsDom = function () {
            var t_ = $("div[name='searchColums']");
            var span = '<span style="padding-left: 20px">使用记录:（共<span id="total"></span>张，未回收<span id="recovery"></span>张，已回收<span id="noRecovery"></span>张，已挂失<span id="report"></span>张） </span>'
            t_.append(span);
        }

        //添加查询条件
        var appendHidden = function(){
            var form =  $("#oilcardrecoveryListForm");
            var prjectDom = '<input type="hidden" id="projects" name="projects">'
            var prjectUserDom = '<input type="hidden" id="projectUsers" name="projectUsers">'
            var isqueryDom = '<input type="hidden"  name="isquery" value="1">'
            form.append(prjectDom);
            form.append(prjectUserDom);
            form.append(isqueryDom);
        }

        //油卡使用情况
        var statistics = function () {
            $.ajax({
                type: "POST",
                url: "mallShopCardruleController.do?statistics",
                dataType: "json",
                success: function (data) {
                    if (data.success) {
                        $("#total").html(data.obj.total);
                        $("#recovery").html(data.obj.recovery);
                        $("#noRecovery").html(data.obj.noRecovery);
                        $("#report").html(data.obj.report);
                    } else {
                        tip(data.msg);
                    }
                }
            });
        }
        //初始化项目
        var initProject = function () {
            $.ajax({
                type: "POST",
                url: "mallProjectController.do?loadall",
                dataType: "json",
                success: function (data) {
                    if (data.success) {
                        $("input[comboname='projectName']").combobox('clear');
                        $("input[comboname='projectName']").combobox('loadData', data.obj == null ? [] : data.obj);
                    } else {
                        tip(data.msg);
                    }
                }
            });
        }

        //初始化Combobox
        var initCombobox = function(){
            $("input[name='projectName']").combobox({
                valueField:'id',
                textField:'projectname',
                panelHeight: 200,
                editable: false,
                multiple:true,
                formatter: function (row) {
                    var opts = $(this).combobox('options');
                    return '<input type="checkbox" id="pro_'+row[opts.valueField]+'" value="'+row[opts.valueField]+'" class="combobox-checkbox">' + row[opts.textField]
                },
                onSelect: function (row) { //选中一个选项时调用
                    var opts = $(this).combobox('options');
                    $("#pro_"+row[opts.valueField]).attr('checked', true);
                    projectCharge($(this).combobox('getValues'));
                },
                onUnselect: function (row) {//不选中一个选项时调用
                    var opts = $(this).combobox('options');
                    $("#pro_"+row[opts.valueField]).attr('checked', false);
                    projectCharge($(this).combobox('getValues'));
                },
            });

            $("input[name='projectUserName']").combobox({
                valueField:'id',
                textField:'realname',
                panelHeight: 200,
                editable: false,
                multiple:true,
                formatter: function (row) {
                    var opts = $(this).combobox('options');
                    return '<input type="checkbox" id="user_'+row[opts.valueField]+'" value="'+row[opts.valueField]+'" class="combobox-checkbox">' + row[opts.textField]
                },
                onSelect: function (row) {
                    var opts = $(this).combobox('options');
                    $("#user_"+row[opts.valueField]).attr('checked', true);
                    var selected = $(this).combobox('getValues');
                    var prjectUsers = "";
                    for(var i = 0; i < selected.length; i++){
                        if(i == selected.length -1){
                            prjectUsers =  prjectUsers + selected[i];
                        }else{
                            prjectUsers =  prjectUsers + selected[i] + ",";
                        }
                    }
                    $("#projectUsers").val(prjectUsers);
                },
                onUnselect: function (row) {
                    var opts = $(this).combobox('options');
                    $("#user_"+row[opts.valueField]).attr('checked', false);
                    var selected = $(this).combobox('getValues');
                    if(selected.length > 0 ){
                        var prjectUsers = "";
                        for(var i = 0; i < selected.length; i++){
                            if(i == selected.length -1){
                                prjectUsers =  prjectUsers + selected[i];
                            }else{
                                prjectUsers =  prjectUsers + selected[i] + ",";
                            }
                        }
                        $("#projectUsers").val(prjectUsers);
                    }else{
                        $("#projectUsers").val(null);
                    }
                },
            });

        }
        //项目改变
        var projectCharge = function (projectId) {
                if (null != projectId && projectId.length > 0 && typeof (projectId) != "undefined") {
                    var projectIdStr = "";
                    for(var i = 0; i < projectId.length; i++){
                        if(i == projectId.length -1){
                            projectIdStr =  projectIdStr + projectId[i];
                        }else{
                            projectIdStr =  projectIdStr + projectId[i] + ",";
                        }
                    }
                    //给项目赋值
                    $("#projects").val(projectIdStr);
                    $.ajax({
                        type: "POST",
                        url: "mallProjectController.do?getprojectuser",
                        dataType: "json",
                        data: {"projectId": projectIdStr},
                        success: function (data) {
                            if (data.success) {
                                $("input[comboname='projectUserName']").combobox('clear');
                                $("input[comboname='projectUserName']").combobox('loadData',data.obj == null ? [] : data.obj);
                            } else {
                                tip(data.msg);
                            }
                        }
                    });
                }else{
                    $("input[comboname='projectUserName']").combobox('clear');
                    $("input[comboname='projectUserName']").combobox('loadData',[]);
                    $("#projects").val("");
                    $("#projectUsers").val("");
                }
        }
        //批量更新状态
        var batchUpdateState = function (ids, state, msg) {
            $.dialog.confirm(msg, function () {
                $.ajax({
                    url: 'mallShopCardruleController.do?batchupstate',
                    type: 'post',
                    data: {
                        ids: ids,
                        state: state
                    },
                    cache: false,
                    success: function (data) {
                        var d = $.parseJSON(data);
                        if (d.success) {
                            window.location.href = 'mallShopCardruleController.do?oilcardrecoverylist';
                        } else {
                            tip(d.msg);
                        }
                    }
                });
            }, function () {

            });

        }

        var module = {
            init: function () {
                initDate();
                initCombobox();
                initProject();
                setStatisticsDom();
                statistics();
                appendHidden();
            },
            //导出文件
            exportFile: function () {
                window.location.href = 'mallShopCardruleController.do?oilcardrecoveryexport&' + $("#oilcardrecoveryListForm").serialize();
            },

            //批量回收
            batchRecovery: function () {
                var rowsData = $('#oilcardrecoveryList').datagrid('getSelections');
                if (!rowsData || rowsData.length == 0) {
                    tip('请选择油卡');
                    return;
                }
                var ids = "";
                var temp = true;
                $(rowsData).each(function (index) {
                    if (this.state != 0) {
                        tip('所选油卡中存在已回收或已挂失的油卡，不能批量回收');
                        temp = false;
                        return;
                    } else {
                        if (index == rowsData.length - 1) {
                            ids += this.id;
                        } else {
                            ids += this.id + ",";
                        }
                    }
                });
                if (temp) {
                    batchUpdateState(ids, 1, "确认将选中油卡状态改为回收状态?");
                }
            },
            //挂失
            report: function (id) {
                batchUpdateState(id, 3, "确认将该油卡状态改为挂失状态?");
            },
            //回收
            recory: function (id) {
                batchUpdateState(id, 1, "确认将该油卡状态改为回收状态?");
            },
            //未回收
            norecory: function (id) {
                batchUpdateState(id, 0, "确认将该油卡状态改为未回收状态?");
            },
            useState: function (value, row, index) {
                if (value < 30) {
                    return "正常使用";
                }
                if (value > 30) {
                    return "超过" + value + "天未使用";
                }
            },
            detail: function (id) {
                window.location.href = 'mallShopCardruleController.do?detail&id=' + id;
            },
            //改变背景色
            initBackground: function (index, row) {
                if (row.lastUseDay > 30 && row.state != 3) {
                    return 'background-color:#f3c0c0;';
                }
                if (row.state == 3) {
                    return 'background-color:#94d399;';
                }
            }
        }
        return module;
    })();
</script>

<div class="easyui-layout" fit="true">
    <div region="center" style="padding:0px;border:0px;height: 100px">
        <t:datagrid name="oilcardrecoveryList" title="油卡回收报表" actionUrl="mallShopCardruleController.do?oilcardrecoverydatagrid" idField="id"
                    fit="true" queryMode="group" checkbox="true" rowStyler="mallOilcardRecoveryModule.initBackground">
            <t:dgCol title="编号" field="id" hidden="true"></t:dgCol>
            <t:dgCol title="油卡卡号" field="cardNo" query="true" width="120"></t:dgCol>
            <t:dgCol title="司机" field="driver" width="120"></t:dgCol>
            <t:dgCol title="车辆" field="vehicleNo" width="120"></t:dgCol>
            <t:dgCol title="订单号" field="orderNo" query="true" width="120"></t:dgCol>
            <t:dgCol title="运单号" field="waybillNumber" query="true" width="120"></t:dgCol>
            <t:dgCol title="使用时间" field="createTime" query="true" queryMode="group" formatter="yyyy-MM-dd hh:mm:ss" width="120"></t:dgCol>
            <t:dgCol title="货主" field="consignor" width="120"></t:dgCol>
            <t:dgCol title="项目组" field="projectName" query="true"  width="120"></t:dgCol>
            <t:dgCol title="项目人" field="projectUserName" query="true"  width="120"></t:dgCol>
            <t:dgCol title="状态" field="state" query="true" replace="未回收_0,已回收_1,已挂失_3" width="120"></t:dgCol>
            <t:dgCol title="使用状态" field="lastUseDay" formatterjs="mallOilcardRecoveryModule.useState" width="120"></t:dgCol>
            <t:dgCol title="操作" field="opt" width="100"></t:dgCol>
            <t:dgFunOpt funname="mallOilcardRecoveryModule.report(id)" title="挂失" exp="state#ne#3"></t:dgFunOpt>
            <t:dgFunOpt funname="mallOilcardRecoveryModule.recory(id)" title="回收" exp="state#eq#0"></t:dgFunOpt>
            <t:dgFunOpt funname="mallOilcardRecoveryModule.norecory(id)" title="未回收" exp="state#eq#1"></t:dgFunOpt>
            <t:dgFunOpt funname="mallOilcardRecoveryModule.detail(id)" title="查看"></t:dgFunOpt>
            <t:dgToolBar title="导出" icon="icon-add" funname="mallOilcardRecoveryModule.exportFile"></t:dgToolBar>
            <t:dgToolBar title="批量回收" icon="icon-add" funname="mallOilcardRecoveryModule.batchRecovery"></t:dgToolBar>
        </t:datagrid>
    </div>
</div>